package com.rokyinfo.generator;

import com.rokyinfo.common.utils.DateUtils;
import org.apache.commons.lang.StringUtils;

import java.util.Date;

/**
 * Created by yuanzhijian on 2017/2/4.
 */
public class RecordNumGenerator {

    /**
     * 生产记录编号
     * @param productModel
     * @param lastOrderNum
     * @param quantity
     * @return
     */
    public static String generateOrderNum(String productModel, String lastOrderNum, int quantity) {

        if ( StringUtils.isEmpty(lastOrderNum) ){
            throw new IllegalArgumentException("最后生成的SN号非法");
        }

        String time = DateUtils.format(new Date(), "yyyyMMdd");

        String serialNum = "01";

        // 获取中间的时间
        String serialTime = lastOrderNum.substring(
                lastOrderNum.indexOf("-") + 1,
                lastOrderNum.lastIndexOf("-"));
        int serial = Integer.parseInt(serialTime.substring(8, 10));
        serialNum = String.valueOf(serial + 1);
        while (serialNum.length() < 2) {
            serialNum = "0" + serialNum;
        }

        return productModel + "-" + time + serialNum + "-" + quantity;
    }

}
